package com.ruoyi.airsystem.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.airsystem.domain.AirWarnings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.airsystem.mapper.AirFormaldehydeMapper;
import com.ruoyi.airsystem.domain.AirFormaldehyde;
import com.ruoyi.airsystem.service.IAirFormaldehydeService;
import com.ruoyi.airsystem.mapper.AirWarningsMapper;
/**
 * 甲醛浓度记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-18
 */
@Service
public class AirFormaldehydeServiceImpl implements IAirFormaldehydeService
{
    @Autowired
    private AirFormaldehydeMapper airFormaldehydeMapper;
    @Autowired
    private AirWarningsMapper airWarningsMapper;
    /**
     * 查询甲醛浓度记录
     *
     * @param recordId 甲醛浓度记录主键
     * @return 甲醛浓度记录
     */
    @Override
    public AirFormaldehyde selectAirFormaldehydeByRecordId(Long recordId)
    {
        return airFormaldehydeMapper.selectAirFormaldehydeByRecordId(recordId);
    }

    /**
     * 查询甲醛浓度记录列表
     *
     * @param airFormaldehyde 甲醛浓度记录
     * @return 甲醛浓度记录
     */
    @Override
    public List<AirFormaldehyde> selectAirFormaldehydeList(AirFormaldehyde airFormaldehyde)
    {
        return airFormaldehydeMapper.selectAirFormaldehydeList(airFormaldehyde);
    }

    /**
     * 新增甲醛浓度记录
     *
     * @param airFormaldehyde 甲醛浓度记录
     * @return 结果
     */
    @Override
    public int insertAirFormaldehyde(AirFormaldehyde airFormaldehyde)
    {
        return airFormaldehydeMapper.insertAirFormaldehyde(airFormaldehyde);
    }

    /**
     * 修改甲醛浓度记录
     *
     * @param airFormaldehyde 甲醛浓度记录
     * @return 结果
     */
    @Override
    public int updateAirFormaldehyde(AirFormaldehyde airFormaldehyde)
    {
        return airFormaldehydeMapper.updateAirFormaldehyde(airFormaldehyde);
    }

    /**
     * 批量删除甲醛浓度记录
     *
     * @param recordIds 需要删除的甲醛浓度记录主键
     * @return 结果
     */
    @Override
    public int deleteAirFormaldehydeByRecordIds(Long[] recordIds)
    {
        return airFormaldehydeMapper.deleteAirFormaldehydeByRecordIds(recordIds);
    }

    /**
     * 删除甲醛浓度记录信息
     *
     * @param recordId 甲醛浓度记录主键
     * @return 结果
     */
    @Override
    public int deleteAirFormaldehydeByRecordId(Long recordId)
    {
        return airFormaldehydeMapper.deleteAirFormaldehydeByRecordId(recordId);
    }

    
   public void checkAndInsertWarning(AirFormaldehyde airFormaldehyde) {
    try {
        BigDecimal formaldehydeLevel = airFormaldehyde.getFormaldehydeLevel();

        // 甲醛浓度阈值(60μg/m³)，转换为BigDecimal
        BigDecimal threshold = new BigDecimal("80.0");

        // 使用compareTo方法比较大小
        if (formaldehydeLevel.compareTo(threshold) > 0) {
            // 插入警告记录
            AirWarnings warning = new AirWarnings();
            warning.setDeviceId(airFormaldehyde.getDeviceId());
            warning.setWarningMessage("甲醛浓度超标！当前值：" + formaldehydeLevel + "μg/m³");
            warning.setWarningTime(new Date());
            airWarningsMapper.insertAirWarnings(warning);
        }
    } catch (Exception e) {
        // 异常处理（可记录日志）
        e.printStackTrace();
    }
}

}
